package com.aiju.base.dialog;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author: 佐为
 * @date: 2016-11-10 09:53
 * @version: V1.0
 * @Description: Dialog基类
 */

public abstract class BaseDialog {

    protected Context mContext;

    protected LayoutInflater mLayoutInflater;

    private Dialog mDialog;

    @TargetApi(Build.VERSION_CODES.CUPCAKE)
    public BaseDialog(Context context) {
        this.mContext = context;
        this.mLayoutInflater = LayoutInflater.from(context);

        if (getDialogStyleId() == 0) {
            mDialog = new Dialog(context);
        } else {
            mDialog = new Dialog(context, getDialogStyleId());
        }
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(getView());
        mDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    }

    protected abstract int getDialogStyleId();

    protected abstract View getView();

    public BaseDialog setCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }

    public BaseDialog setSystemAlert() {
        mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
        return this;
    }

    public BaseDialog setCanceledOnTouchOutside(boolean touch) {
        mDialog.setCanceledOnTouchOutside(touch);
        return this;
    }

    public void show() {
        mDialog.show();
    }

    public void dismiss() {
        mDialog.dismiss();
    }

    public boolean isShowing() {
        return mDialog.isShowing();
    }

    public BaseDialog setdismissListeren(DialogInterface.OnDismissListener dismissListener) {
        mDialog.setOnDismissListener(dismissListener);
        return this;
    }

    protected Window getDialogWindow() {
        return mDialog.getWindow();
    }
}
